package xio.ccf_201709;

import java.util.*;

/**
 * 通信网络 100分
 * Created by Xio on 2017/10/29.
 */
public class CommunicatingNet {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        Digraph g = new Digraph(n);
        for (int i = 1; i <= m; i++) {
            int v = sc.nextInt();
            int w = sc.nextInt();
            g.addEdge(v,w);
        }
        Kosaraju kosaraju = new Kosaraju(g);
        int sccCount = kosaraju.count();
        np = new int[n+1];
        nc = new int[n+1];


        for (int i = 0; i < sccCount; i++) {
            marked = new boolean[n+1];
            mainDfs(kosaraju,g,kosaraju.sscV(i),i,np);
        }
        Digraph reG = g.reverse();
        for (int i = 0; i < sccCount; i++) {
            marked = new boolean[n+1];
            mainDfs(kosaraju,reG,kosaraju.sscV(i),i,nc);
        }
//        System.out.println("count:"+sccCount);
        int result = 0;
        for (int i = 1; i <= n; i++) {
//            System.out.println(" v: "+kosaraju.sscV(i));
//            System.out.println(np[i]+"  "+nc[i]);
            if (np[i]+nc[i]+1 == sccCount) result++;
        }
        System.out.print(""+result);

    }
    static boolean[] marked;
    static int[] np;
    static int[] nc;
    static void mainDfs(Kosaraju kosaraju,Digraph g,int v,int id,int[] re){
        if (kosaraju.id(v) != id) re[v]++;
        marked[v] = true;
        for (Integer i : g.adj(v)) {
            if (!marked[i]){
                mainDfs(kosaraju,g,i,id,re);
            }
        }
    }

    static class Digraph{
        private int V;
        private int E;
        private List<LinkedHashSet<Integer>> adj;
        Digraph(int N){
            this.V = N;
            this.E = 0;
            adj = new ArrayList<>();
            for (int i = 0; i <= V; i++) {
                adj.add(new LinkedHashSet<Integer>());
            }
        }

        public void addEdge(int v,int w){
            adj.get(v).add(w);
            E++;
        }

        public LinkedHashSet<Integer> adj(int v){
           return adj.get(v);
        }

        public Digraph reverse(){
            Digraph d = new Digraph(V);
            for (int i = 1; i <= V; i++) {
                for (int w : adj(i)) {
                    d.addEdge(w,i);
                }
            }
            return d;

        }

        public int V(){
            return V;
        }

        public int E(){
            return E;
        }
    }

    static class DepthFirstOrder{
        private boolean[] marked;
        private Stack<Integer> reversePost;

        DepthFirstOrder(Digraph g){
            reversePost = new Stack<>();
            marked = new boolean[g.V()+1];
            for (int i = 1; i <= g.V(); i++) {
               if (!marked[i]) dfs(g,i);
            }
        }

        private void dfs(Digraph g,int v){
            marked[v] = true;
            for (Integer i : g.adj(v)) {
                if (!marked[i]){
                    dfs(g,i);
                }
            }
            reversePost.push(v);
        }

        public Stack<Integer> reversePost(){
            return reversePost;
        }
    }

    static class Kosaraju{
        private boolean[] marked;
        private int[] id;
        private int count = 0;
        private int[] sscV;

        Kosaraju(Digraph g){
            marked = new boolean[g.V()+1];
            id = new int[g.V()+1];
            sscV = new int[g.V()+1];
            DepthFirstOrder order = new DepthFirstOrder(g.reverse());
            Stack<Integer> reversePost = order.reversePost();
           while (!reversePost.isEmpty()){
               int i = reversePost.pop();
                if (!marked[i]) {
                    dfs(g,i);
                    count++;
                }
            }
        }

        private void dfs(Digraph g, int v) {
            marked[v] = true;
            id[v] = count;
            sscV[count] = v;
            for (Integer i : g.adj(v)) {
                if (!marked[i]){
                    dfs(g,i);
                }
            }
        }

        public int sscV(int id){
            return sscV[id];
        }

        public int id(int v){
            return id[v];
        }
        public int count(){
            return count;
        }
    }

}
